<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;

class Controller extends BaseController
{
    use AuthorizesRequests, ValidatesRequests;

    /**
     * 返回成功
     * @param array $data
     * @return JsonResponse
     */
    public function retSuccess(array $data = []): JsonResponse
    {
        return response()->json([
            'code' => 0,
            'data' => $data,
            'msg' => 'success',
        ]);
    }

    /**
     * 返回失败
     * @param int $code
     * @param string $msg
     * @param array $data
     * @param int $status
     * @return JsonResponse
     */
    public function retError(int $code = -1, string $msg = '', array $data = [], int $status = 200): JsonResponse
    {
        return response()->json([
            'code' => $code,
            'data' => $data,
            'msg' => $msg,
        ], $status);
    }

    /**
     * 从jwt中获取uid
     * @param Request $request
     * @return int
     */
    protected function getUid(Request $request): int
    {
        return empty($request->jwt['uid']) ? 0 : (int)$request->jwt['uid'];
    }
}
